# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from scrapy.pipelines.images import ImagesPipeline, FilesPipeline
import scrapy
from . import utils, settings

PASS_LIST = {
    "comic": ('112298','111868','111830','153','173'),
    "DM": (),
    "3D": (),
    "coterie": (),
    "motion": (),
}
PASS_AUTHOR = {
    "comic": ('Webtoon'),
    "DM": (),
    "3D": (),
    "coterie": (),
    "motion": (),
}

# 下载图片管道
class ComicsImagePipeline(ImagesPipeline):
    debug_mode = False
    # 取得图片请求
    def get_media_requests(self, item, info):
        topic = item.get('topic', '')
        if topic == 'comic':
            id = item.get('id', '')
            author = utils.check_author(item.get('author', ''))
            if author == '':
                author = "other"
            if (id in PASS_LIST[topic]) and (author not in PASS_AUTHOR[topic]):
                item['is_pass'] = True
            else:
                pages = int(item.get('pages', '0'))
                image_url = item.get('image_url', '')
                total = utils.check_directory(topic, author, id, settings.ROOT_PATH)
                if pages > 0 and total < pages:
                    info = utils.get_info(author, id, image_url)
                    for page in range(pages):
                        info['page_no'] = page
                        result = utils.check_file(topic, info, settings.ROOT_PATH)
                        if result["new_url"] != "":
                            yield scrapy.Request(result["new_url"], meta=result)
    
    # 指定图片的档案名称
    def file_path(self, request, response=None, info=None):
        if self.debug_mode:
            print(f'ComicsImagePipeline.file_path folder={request.meta["folder"]}, file_name={request.meta["file_name"]}, new_url={request.meta["new_url"]}')
        file_name = request.meta['file_name'] or ''
        return file_name

    # 结束下载
    def item_completed(self, results, item, info):
        return item
    

# # 保存视频资料
class VideoPipeline(FilesPipeline):
    debug_mode = False
    # 取得请求
    def get_media_requests(self, item, info):
        topic = item.get('topic', '')
        id = item.get('id', '')
        title = item.get('title', '')
        group = item.get('group', '')
        tag = item.get('tag', [])
        author = utils.check_author(item.get('author', ''))
        if author == '':
            author = "other"
        if topic != 'comic':
            if id in PASS_LIST[topic] or ('只有男性' in tag) or ('耽美' in tag):
                item['is_pass'] = True
            else:
                download_list = item.get('download_list', [])
                if len(download_list)>0 and download_list[0] is not None and download_list[0] != "":
                    download_url = download_list[0]
                    folder = group
                    if folder == "":
                        folder = id
                    # 检查目录、档案是否存在、获取视频链接
                    if self.debug_mode:
                        print(f'VideoPipeline.get_media_requests topic={topic}, author={author}, id={id}, download_url={download_url}')
                    total = utils.check_directory(topic, author, folder, settings.ROOT_PATH)
                    info = utils.get_info(author, folder, download_url, title)
                    result = utils.check_file(topic, info, settings.ROOT_PATH)
                    if result["new_url"] != "":
                        # 下载视频文件
                        if self.debug_mode:
                            print(f'VideoPipeline.get_media_requests2 folder={result["folder"]}, file_name={result["file_name"]}, new_url={result["new_url"]}')
                        yield scrapy.Request(result["new_url"], meta=result)
                    else:
                        #print(f'PASS id={id}, title={title}')
                        pass

    # 指定档案名称
    def file_path(self, request, response=None, info=None):
        if self.debug_mode:
            print(f'VideoPipeline.file_path folder={request.meta["folder"]}, file_name={request.meta["file_name"]}, new_url={request.meta["new_url"]}')
        file_name = request.meta['file_name'] or ''
        return file_name

    # 结束下载
    def item_completed(self, results, item, info):
        #if self.debug_mode:
        if len(results) > 0:
            download_result = results[0][0]
            item["download_result"] = download_result
            print(f'COMPLETED id={item["id"]}, result={download_result}, title={item["title"]}')
        return item
